package ldk.easytouch.activity;

import android.content.Context;
import android.graphics.Rect;
import android.support.annotation.NonNull;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;

import ldk.easytouch.helper.InstrumentHelper;
import ldk.easytouch.helper.PositionHelper;
import ldk.easytouch.interfaces.IActivity;
import ldk.easytouch.utils.ETLog;

/**
 * Created by ldkxingzhe on 2016/9/13.
 */
public class BaseActivity implements IActivity{
    @SuppressWarnings("unused")
    private static final String TAG = "BaseActivity";

    private View mContentView;
    private InstrumentHelper mInstrumentHelper;

    @Override
    public void onCreate() {
        ETLog.v(TAG, "onCreate");
        mInstrumentHelper = InstrumentHelper.getInstance();
    }

    /* called by InstrumentHelper */
    @Override
    public boolean onUpdateWindowLayoutParams(WindowManager.LayoutParams params){
        return true;
    }

    protected void setFlagNotTouchOutSide(WindowManager.LayoutParams params){
        params.flags = WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL
                | WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH
                | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
                | WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN
                | WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS;
    }

    protected void setFlagTouchableOutSide(WindowManager.LayoutParams params){
        params.flags &= ~WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL;
        params.flags &= ~WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
    }

    @Override
    public void onResume() {
        ETLog.v(TAG, "onResume");
    }

    @Override
    public void onPause() {
        ETLog.v(TAG, "onPause");
    }

    @Override
    public void onStop() {
        ETLog.v(TAG, "onStop");
    }

    @Override
    public void onDestroy() {
        ETLog.v(TAG, "onDestroy");
    }

    @NonNull
    protected Rect adjustCenter(WindowManager.LayoutParams params) {
        Rect rect = PositionHelper.getInstance().calculateRect(getContentView().getMeasuredWidth(),
                getContentView().getMeasuredHeight());
        params.x = rect.left;
        params.y = rect.top;
        return rect;
    }

    @Override
    public void finish() {
        mInstrumentHelper.onFinish(this);
    }

    @Override
    public void onTouchOutside() {
        ETLog.v(TAG, "onTouchOutside");
        finish();
    }

    @Override
    public void onKeyDown(int keyCode, KeyEvent event) {
        if(keyCode == KeyEvent.KEYCODE_BACK){
            onBackPressed();
        }
    }

    public void onBackPressed(){
        finish();
    }

    public Context getContext(){
        return mInstrumentHelper.getRootView().getContext();
    }

    public <T> T findView(int id){
        return (T) mInstrumentHelper.getRootView().findViewById(id);
    }

    protected void setContentView(int layoutId){
        View view = LayoutInflater.from(getContext()).inflate(layoutId, mInstrumentHelper.getRootView(), false);
        setContentView(view);
    }

    @Override
    public void setContentView(@NonNull View view, int width, int height) {
        ETLog.v(TAG, "setContentView");
        mContentView = view;
        mInstrumentHelper.addView(view);
    }

    @Override
    public void setContentView(@NonNull View view) {
        ETLog.v(TAG, "setContentView");
        ViewGroup.LayoutParams params = view.getLayoutParams();
        setContentView(view, params.width, params.height);
    }

    @Override
    public View getContentView() {
        if(mContentView == null)
            throw new IllegalStateException("You must call onCreate before call setContentView");
        return mContentView;
    }

    public void startActivity(@NonNull IActivity activity) {
        ETLog.v(TAG, "startActivity");
        mInstrumentHelper.onStartActivity(activity, null);
    }
}
